﻿using System.Collections.Generic;
using System.Threading.Tasks;
namespace Odyssey.Consul.Implementations
{
    using Features;
    using Http;
    using Models;
    public class Health : BaseConsulAPI, IHealth
    {
        public Health(IHttpRequester httpRequester, IConsulConfiguration consulConfiguration) : base(httpRequester, consulConfiguration, EnumResource.Service)
        {
        }

        private string BuildHealthPath(string action)
        {
            return BuildUri("health", action);
        }

        private QueryParameter BuidQueryParameter(ArguHealth arguHealth)
        {
            QueryParameter queryParameter = null;
            if (arguHealth != null)
            {
                queryParameter = new QueryParameter();
                if (!string.IsNullOrWhiteSpace(arguHealth.DC))
                {
                    queryParameter.Datacenter = arguHealth.DC;
                }
                if (!string.IsNullOrWhiteSpace(arguHealth.Near))
                {
                    queryParameter.OtherParameters.Add("near", arguHealth.Near);
                }
                if (!string.IsNullOrWhiteSpace(arguHealth.NodeMeta))
                {
                    queryParameter.OtherParameters.Add("node-meta", arguHealth.NodeMeta);
                }
                if (!string.IsNullOrWhiteSpace(arguHealth.Passing))
                {
                    queryParameter.OtherParameters.Add("passing", arguHealth.Passing);
                }
                if (!string.IsNullOrWhiteSpace(arguHealth.Tag))
                {
                    queryParameter.OtherParameters.Add("tag", arguHealth.Tag);
                }
            }
            return queryParameter;
        }

        public async Task<List<ResultCheck>> ListChecksForNode(ArguHealth arguHealth)
        {
            var path = BuildHealthPath($"node/{arguHealth.Node}");
            return await ApiSend<List<ResultCheck>>(path, queryParameter: BuidQueryParameter(arguHealth), requestHeaders: BuildTokenHeader(new EnumResource[] {
                 EnumResource.Node,
                 EnumResource.Service
            }));
        }

        public async Task<List<ResultCheck>> ListChecksForService(ArguHealth arguHealth)
        {
            var path = BuildHealthPath($"checks/{arguHealth.Service}");
            return await ApiSend<List<ResultCheck>>(path, queryParameter: BuidQueryParameter(arguHealth), requestHeaders: BuildTokenHeader(new EnumResource[] {
                 EnumResource.Node,
                 EnumResource.Service
            }));
        }

        public async Task<List<ResultCheck>> ListChecksInState(ArguHealth arguHealth)
        {
            var path = BuildHealthPath($"state/{arguHealth.State}");
            return await ApiSend<List<ResultCheck>>(path, queryParameter: BuidQueryParameter(arguHealth), requestHeaders: BuildTokenHeader(new EnumResource[] {
                 EnumResource.Node,
                 EnumResource.Service
            }));
        }

        public async Task ListNodesForConnectCapableService(ArguHealth arguHealth)
        {
            var path = BuildHealthPath($"connect/{arguHealth.Service}");
            await ApiSendResponse(path, EnumConsulHttpMethod.Get);
        }

        public async Task<List<ResultListNodesForSerivce>> ListNodesForSerivce(ArguHealth arguHealth)
        {
            var path = BuildHealthPath($"service/{arguHealth.Service}");
            return await ApiSend<List<ResultListNodesForSerivce>>(path, queryParameter: BuidQueryParameter(arguHealth), requestHeaders: BuildTokenHeader(new EnumResource[] {
                 EnumResource.Node,
                 EnumResource.Service
            }));
        }
    }
}
